rm(list=ls())
library(tidyverse)
library(electionsBR)

# Create folders to receive data
dir.create(here::here("data","raw","TSE"))
dir.create(here::here("data","raw","TSE","campaign_finance"))


# Date of download
date <- Sys.Date()

# Download Campaign Finance Data ----------

# Download prestacao de contas 2004
prest_contas_2004_url <-"http://agencia.tse.jus.br/estatistica/sead/odsele/prestacao_contas/prestacao_contas_2004.zip"
download.file(prest_contas_2004_url, here::here("data","raw","TSE","campaign_finance",paste0("prest_contas_2004_downloaded_",date,".zip")))

# Download prestacao de contas 2008
prest_contas_2008_url <-"http://agencia.tse.jus.br/estatistica/sead/odsele/prestacao_contas/prestacao_contas_2008.zip"
download.file(prest_contas_2008_url, here::here("data","raw","TSE","campaign_finance",paste0("prest_contas_2008_downloaded_",date,".zip")))

# Download prestacao de contas 2012
prest_contas_2012_url <-"http://agencia.tse.jus.br/estatistica/sead/odsele/prestacao_contas/prestacao_final_2012.zip"
download.file(prest_contas_2012_url, here::here("data","raw","TSE","campaign_finance",paste0("prest_final_2012_downloaded_",date,".zip")))

# Download Self-Reported Wealth Data ----------

# Bens candidatos 2008
bens_candidatos_2008_url <- "http://agencia.tse.jus.br/estatistica/sead/odsele/bem_candidato/bem_candidato_2008.zip"
download.file(bens_candidatos_2008_url, here::here("data","raw","TSE","campaign_finance",paste0("bem_candidato_2008_downloaded_",date,".zip")))

# Bens candidatos 2012
bens_candidatos_2012_url <- "http://agencia.tse.jus.br/estatistica/sead/odsele/bem_candidato/bem_candidato_2012.zip"
download.file(bens_candidatos_2012_url, here::here("data","raw","TSE","campaign_finance",paste0("bem_candidato_2012_downloaded_",date,".zip")))

# Download Electoral Results ----------
# Function to Download Results of Mayoral Elections 

download <- function(year,save_path){
  
  data <- vote_mun_zone_local(year)
  
  data <- data %>% 
    mutate_at(vars(NOME_CANDIDATO,DESC_SIT_CAND_TOT,DESC_SIT_CANDIDATO,NOME_COLIGACAO,DESCRICAO_ELEICAO,NOME_MUNICIPIO,DESCRICAO_CARGO),
              list(~stringi::stri_trans_general(., "latin-ascii"))) %>% 
    rename_all(tolower)
  
  write_rds(data, path = paste0(save_path,"/mun_elec_",year,"_downloaded_",date,".rds"))
}

possibly_download <- possibly(download,otherwise = "Error when downloading")

# Apply function
years <- c(1996,2000,2004,2008,2012,2016)

map(years,possibly_download,save_path=here::here("data","raw","TSE"))

# Download Coalition data ----------
# Function to Download Coalitions in Mayoral Elections 

download <- function(year,save_path){
  
  data <- legend_local(year)
  
  data <- data %>% 
    mutate_at(vars(DESCRICAO_ELEICAO,NOME_MUNICIPIO,TIPO_LEGENDA,NOME_PARTIDO,NOME_COLIGACAO),
              list(~stringi::stri_trans_general(., "latin-ascii"))) %>% 
    rename_all(tolower)
  
  write_rds(data, path = paste0(save_path,"/mun_coalitions_",year,"_downloaded_",date,".rds"))
}

possibly_download <- possibly(download,otherwise = "Error when downloading")

# Aplly function

years <- c(2000,2004,2008,2012,2016)

map(years,possibly_download,save_path=here::here("data","raw","TSE"))

# Download Candidate Characteristics data ----------
# Function to Download Candidate Characteristics 
download <- function(year,save_path){
  
  data <- candidate_local(year)
  
  # Harmonize data types to be able to bind later
  data <- data %>%
    mutate_if(is.character,
              list(~stringi::stri_trans_general(., "latin-ascii"))) %>%
    mutate_all(as.character) %>% 
    rename_all(tolower) 
  
  if (year == 2016){
    data <- data %>% 
      mutate_at(vars(ano_eleicao,num_turno,numero_candidato,numero_partido,idade_data_posse,despesa_max_campanha),
                list(~as.numeric(.)))  
  } else {
    data <- data %>% 
      mutate_at(vars(ano_eleicao,num_turno,numero_candidato,numero_partido,idade_data_eleicao,despesa_max_campanha),
                list(~as.numeric(.)))  
  }

  write_rds(data, path = paste0(save_path,"/candidate_chars_",year,"_downloaded_",date,".rds"))
}

possibly_download <- possibly(download,otherwise = "Error when downloading")

# Apply function
years <- c(1996,2000,2004,2008,2012,2016)

map(years,possibly_download,save_path=here::here("data","raw","TSE"))